package com.yongbao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yongbao.model.domain.DishOrderSubDO;
import com.yongbao.model.request.DishOrderSubRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhanghong
 */
@Repository
public interface DishOrderSubMapper extends BaseMapper<DishOrderSubDO> {
    DishOrderSubDO selectDOById(Long id);
    List<DishOrderSubDO> selectDOList(DishOrderSubDO dishOrderSub);
    int insertDO(DishOrderSubDO dishOrderSub);
    int batchInsertDO(List<DishOrderSubDO> dishOrderSubDOList);
    int updateDO(DishOrderSubDO dishOrderSub);
    int deleteDOById(Long id);
    int deleteDOByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from dish_order_sub left join dish_order on dish_order_sub.order_id=dish_order.id",
            "   <where>",
            "       <if test=\"param.orderId != null\">and dish_order_sub.order_id=#{param.orderId}</if>",
            "       <if test=\"param.createUser != null\">and dish_order_sub.create_user=#{param.createUser}</if>",
            "       <if test=\"param.orderType != null\">and dish_order.order_type=#{param.orderType}</if>",
            "       <if test=\"param.expiredWeekOrMonthDish != null and param.expiredWeekOrMonthDish==1\">and (dish_order.order_type in (1, 2) and end_date &lt; now())</if>",
            "       <if test=\"param.createUser != null\">and dish_order_sub.create_user=#{param.createUser}</if>",
            "       <if test=\"param.areaName!=null and param.areaName!=''\">and dish_order.area_name = #{param.areaName}</if>",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">and (dish_order.area_name like concat('%',#{param.keyWord},'%') or dish_order.street_name like concat('%',#{param.keyWord},'%') or dish_order.address like concat('%',#{param.keyWord},'%'))</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") DishOrderSubRequest param);
    @Select({"<script>",
            "   select dish_order_sub.* from dish_order_sub left join dish_order on dish_order_sub.order_id=dish_order.id",
            "   <where>",
            "       <if test=\"param.orderId != null\">and dish_order_sub.order_id=#{param.orderId}</if>",
            "       <if test=\"param.createUser != null\">and dish_order_sub.create_user=#{param.createUser}</if>",
            "       <if test=\"param.orderType != null\">and dish_order.order_type=#{param.orderType}</if>",
            "       <if test=\"param.expiredWeekOrMonthDish != null and param.expiredWeekOrMonthDish==1\">and (dish_order.order_type in (1, 2) and end_date &lt; now())</if>",
            "       <if test=\"param.createUser != null\">and dish_order_sub.create_user=#{param.createUser}</if>",
            "       <if test=\"param.areaName!=null and param.areaName!=''\">and dish_order.area_name = #{param.areaName}</if>",
            "       <if test=\"param.keyWord!=null and param.keyWord!=''\">and (dish_order.area_name like concat('%',#{param.keyWord},'%') or dish_order.street_name like concat('%',#{param.keyWord},'%') or dish_order.address like concat('%',#{param.keyWord},'%'))</if>",
            "   </where>",
            "   order by dish_order_sub.create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<DishOrderSubDO> pageQuery(@Param("param") DishOrderSubRequest param, @Param("limit") int limit, @Param("offset") long offset);

    Integer countSubOrderByAreaId(Long areaId);

    Integer countSubOrderByAreaIdAndStreetId(@Param("areaId") Long areaId, @Param("streetId") Long streetId);
}
